home *** CD-ROM | disk | FTP | other *** search
Text File | 2010-01-29 | 52.4 KB | 1,561 lines |
- /* ***** BEGIN LICENSE BLOCK *****
- * Version: MPL 1.1/GPL 2.0/LGPL 2.1
- *
- * The contents of this file are subject to the Mozilla Public License Version
- * 1.1 (the "License"); you may not use this file except in compliance with
- * the License. You may obtain a copy of the License at
- * http://www.mozilla.org/MPL/
- *
- * Software distributed under the License is distributed on an "AS IS" basis,
- * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
- * for the specific language governing rights and limitations under the
- * License.
- *
- * The Original Code is WebMail Notifier.
- *
- * The Initial Developer of the Original Code is
- * Byungwook Kang.
- * Portions created by the Initial Developer are Copyright (C) 2007
- * the Initial Developer. All Rights Reserved.
- *
- * Contributor(s):
- *
- * Alternatively, the contents of this file may be used under the terms of
- * either the GNU General Public License Version 2 or later (the "GPL"), or
- * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
- * in which case the provisions of the GPL or the LGPL are applicable instead
- * of those above. If you wish to allow use of your version of this file only
- * under the terms of either the GPL or the LGPL, and not to allow others to
- * use your version of this file under the terms of the MPL, indicate your
- * decision by deleting the provisions above and replace them with the notice
- * and other provisions required by the GPL or the LGPL. If you do not delete
- * the provisions above, a recipient may use your version of this file under
- * the terms of any one of the MPL, the GPL or the LGPL.
- *
- * ***** END LICENSE BLOCK ***** */
-
- /***********************************************************
- constants
- ***********************************************************/
-
- const nsIWebMailNotifier = Components.interfaces.nsIWebMailNotifier;
-
- // UUID uniquely identifying our component
- const CLASS_ID = Components.ID("{e3ab0980-b4e1-11db-abbd-0800200c9a66}");
-
- // description
- const CLASS_NAME = "WebMail Notifier Service";
-
- // textual unique identifier
- const CONTRACT_ID = "@mozilla.org/WebMailNotifier;1";
-
- const ST_PRE = 0;
- const ST_PRE_RES = 1;
- const ST_LOGIN = 10;
- const ST_LOGIN_RES = 11;
- const ST_DATA = 20;
- const ST_DATA_RES = 21;
-
- const LOGIN_URL = 0;
- const LOGIN_ID = 1;
- const LOGIN_PW = 2;
- const LOGIN_DATA = 3;
- const LOGIN_POST = 1;
-
- const formSubmitURL = "user";
-
- const dout=Components.utils.reportError;
-
- var enableDebug=false;
- /***********************************************************
- class definition
- ***********************************************************/
-
- //class constructor
- function WebMailNotifier() {
- var observerService = Components.classes["@mozilla.org/observer-service;1"]
- .getService(Components.interfaces.nsIObserverService);
- observerService.addObserver(this, "http-on-modify-request", false);
- observerService.addObserver(this, "http-on-examine-response", false);
- //for uninstall
- this.uninstall=false;
- observerService.addObserver(this, "em-action-requested", false);
- observerService.addObserver(this, "quit-application-granted", false);
-
- //init preferences
- var prefService = Components.classes["@mozilla.org/preferences-service;1"]
- .getService(Components.interfaces.nsIPrefService);
- this.prefBranch = prefService.getBranch("extensions.wmn.");
- this.prefBranch.QueryInterface(Components.interfaces.nsIPrefBranch2);
- this.prefBranch.addObserver("", this, false);
- this.timerDelay=this.prefBranch.getIntPref("updateInterval")*60000;
- this.connectionDelay=this.prefBranch.getIntPref("connectionDelay")*1000;
- this.newMailsOnly=this.prefBranch.getBoolPref("newMailsOnly");
-
- var tmp=this;
- this.worker={
- notify:function(aTimer){
- WebMailNotifier.prototype.timerWork.call(tmp,aTimer);
- }
- };
- this.timer = Components.classes["@mozilla.org/timer;1"]
- .createInstance(Components.interfaces.nsITimer);
- this.workTimer = Components.classes["@mozilla.org/timer;1"]
- .createInstance(Components.interfaces.nsITimer);
-
-
- enableDebug=this.prefBranch.getIntPref("debug");
-
- if ("@mozilla.org/login-manager;1" in Components.classes){
- this.loginManager = Components.classes["@mozilla.org/login-manager;1"]
- .getService(Components.interfaces.nsILoginManager);
- }else if ("@mozilla.org/passwordmanager;1" in Components.classes){
- this.passwordManager = Components.classes["@mozilla.org/passwordmanager;1"]
- .getService(Components.interfaces.nsIPasswordManager);
- }
- }
- // class definition
- WebMailNotifier.prototype = {
- listeners : new Array(),
- timer : null,
- inited : false,
- initCalled : false,
- countTotal : 0,
- QueryInterface: function(aIID)
- {
- if (!aIID.equals(nsIWebMailNotifier) &&
- !aIID.equals(Components.interfaces.nsISupports))
- throw Components.results.NS_ERROR_NO_INTERFACE;
- return this;
- }
- }
- WebMailNotifier.prototype.init=function() {
- var str="chrome://wmn/options";
- var str2="autoLogin";
- this.autoLogin=this.getPassword(str,str2);
- if(this.autoLogin==null){
- this.autoLogin=this.prefBranch.getBoolPref("autoLoginDefault")?1:0;
- this.setPassword(str,str2,this.autoLogin);
- }else this.autoLogin=parseInt(this.autoLogin);
-
- this.bundle = Components.classes["@mozilla.org/intl/stringbundle;1"]
- .getService(Components.interfaces.nsIStringBundleService)
- .createBundle("chrome://wmn/locale/overlay.properties"),
-
- this.userScripts={};
- this.scripts={};
- this.initScripts(true);
- this.initScripts(false);
-
- this.initHandlers(true);
- if(this.prefBranch.getBoolPref("startupOpenWMN"))this.openWMN();
- }
-
- WebMailNotifier.prototype.initScripts = function(isDefault) {
- var ios = Components.classes["@mozilla.org/network/io-service;1"]
- .getService(Components.interfaces.nsIIOService);
- var fileHandler = ios.getProtocolHandler("file")
- .QueryInterface(Components.interfaces.nsIFileProtocolHandler);
- var loader = Components.classes["@mozilla.org/moz/jssubscript-loader;1"]
- .getService(Components.interfaces.mozIJSSubScriptLoader);
- var file;
- if(isDefault){
- file=__LOCATION__.parent;
- var dir=fileHandler.getURLSpecFromFile(file);
- var info = Components.classes["@mozilla.org/xre/app-info;1"]
- .getService(Components.interfaces.nsIXULAppInfo);
- if(info.name=="Thunderbird"){
- loader.loadSubScript(dir+"ui-tb.js");
- }else{
- loader.loadSubScript(dir+"ui.js");
- }
-
- file.append("scripts");
- this.dir0=fileHandler.getURLSpecFromFile(file);
- try{
- loader.loadSubScript(this.dir0+"order.dat",this);
- }catch(e){dout(e);}
- }else{
- file = Components.classes["@mozilla.org/file/directory_service;1"]
- .getService(Components.interfaces.nsIProperties)
- .get("ProfD", Components.interfaces.nsIFile);
- file.append("wmn");
- this.dir1=fileHandler.getURLSpecFromFile(file);
- if(this.dir1.charAt(this.dir1.length-1)!="/"){//folder not exist
- this.dir1+="/";
- }
- }
- if(file.exists()){
- var entries = file.directoryEntries;
- while(entries.hasMoreElements())
- {
- var t = entries.getNext();
- t.QueryInterface(Components.interfaces.nsIFile);
- var s=t.leafName;
- var fnd;
- if(fnd=s.match(/((lib-)?.+).js$/)){
- if(fnd[2]){//load library
- try{
- loader.loadSubScript((isDefault?this.dir0:this.dir1)+fnd[0]);
- }catch(e){
- dout(fnd[0]+" "+e);
- }
- }else{
- this.scripts[fnd[1]]=null;
- }
- if(!isDefault)this.userScripts[fnd[1]]=true;
- }
- }
- }
- }
- WebMailNotifier.prototype.initHandler = function(obj,id,user,pass) {
- this.webMailHandlers.push(obj);
- obj.id=id;
- obj.user=user;
- obj.password=pass;
- obj.inited=false;
- if(this.order[id])obj.key=this.order[id].toString();
- }
- WebMailNotifier.prototype.initHandlers = function(aCheckNow) {
- //dout("***initHandlers "+aCheckNow);
- this.stopAll();
- this.webMailHandlers=[];
- var str="chrome://wmn/accounts/";
- if(this.loginManager){
- for(var id in this.scripts){
- var logins = this.loginManager.findLogins({},str+id,formSubmitURL, null);
- for each (var o in logins) {
- var obj=new WebMailHandler(this);
- this.initHandler(obj,id,o.username,o.password);
- }
- }
- }else{
- var e = this.passwordManager.enumerator;
- while (e.hasMoreElements()) {
- try {
- var pass = e.getNext().QueryInterface(Components.interfaces.nsIPassword);
- if (pass.host.indexOf(str)==0) {
- var id=pass.host.substring(str.length);
- var obj=new WebMailHandler(this);
- this.initHandler(obj,id,pass.user,pass.password);
- }
- }catch(ex) {}
- }
- }
- this.webMailHandlers.sort(this.sortFunc);
- this.buildTable(aCheckNow);
- }
- WebMailNotifier.prototype.sortFunc = function(a,b) {
- var keyA=a.key?a.key:a.id;
- var keyB=b.key?b.key:b.id;
- if(keyA<keyB)return -1;
- else if(keyA==keyB){
- if(a.user<b.user)return -1;
- else if(a.user==b.user)return 0;
- else return 1;
- }else return 1;
- }
- WebMailNotifier.prototype.loadScript = function(id,isTemp) {
- var loader = Components.classes["@mozilla.org/moz/jssubscript-loader;1"]
- .getService(Components.interfaces.mozIJSSubScriptLoader);
- if(!this.scripts[id]){
- //dout("loadScript "+(this.userScripts[id]?this.dir1:this.dir0)+id+".js");
- var scr={};
- loader.loadSubScript((this.userScripts[id]?this.dir1:this.dir0)+id+".js",scr);
- //dout("loaded "+id+" "+isTemp);
- if(!isTemp)this.scripts[id]=scr;
- //if(!isTemp)dout("load ["+id+"] "+scr.name);
- return scr;
- }else return this.scripts[id];
- }
- WebMailNotifier.prototype.buildTable = function(aCheckNow) {
- this.stopAll();
- //dout("***buildTable "+aCheckNow);
- var loader = Components.classes["@mozilla.org/moz/jssubscript-loader;1"]
- .getService(Components.interfaces.mozIJSSubScriptLoader);
- var i;
- for(i=0;i<this.webMailHandlers.length;++i){
- var obj=this.webMailHandlers[i];
- try{
- obj.enabled=this.prefBranch.getBoolPref("accounts."+obj.id+"."+obj.user+".enabled");
- }catch(e){}
- if(!obj.enabled||obj.inited)continue;
- //dout("init "+obj.id+" "+obj.user);
- try{
- this.loadScript(obj.id);
- var scr=this.scripts[obj.id];
- for(var k in scr){
- obj[k]=scr[k];
- }
- if(obj.supportInboxOnly)obj.inboxOnly=obj.isInboxOnly();
- if(obj.supportShowFolders)obj.showFolders=obj.isShowFolders();;
- if(obj.needServer)obj.server=obj.user.split("|")[1];
- if(obj.init)obj.init();
- if(obj.loginData){
- var post=(obj.loginData[LOGIN_DATA]?obj.loginData[LOGIN_DATA]:"");
- if(obj.loginData[LOGIN_ID]){
- if(post)post+="&";
- var user;
- if(obj.needServer)user=obj.user.split("|")[0];
- else user=obj.user;
- post+=obj.loginData[LOGIN_ID]+"="+encodeURIComponent(user);
- }
- if(obj.loginData[LOGIN_PW]){
- if(post)post+="&";
- post+=obj.loginData[LOGIN_PW]+"="+encodeURIComponent(obj.password);
- }
- obj.loginData.splice(1,obj.loginData.length-1);
- obj.loginData[LOGIN_POST]=post;
- }
- obj.stage=obj.initStage;
- obj.inited=true;
- }catch(e){
- dout(obj.id+" "+e);
- this.webMailHandlers.splice(i,1);//not loaded
- --i;
- }
- }
- for(i=0;i<this.webMailHandlers.length;++i){
- this.webMailHandlers[i].ind=i;
- }
-
- this.hdlTable={};
- var defaults={};
- for(var i in this.scripts){
- this.hdlTable[i]=[];
- try{
- defaults[i]=this.prefBranch.getCharPref("accounts."+i+".default");
- }catch(e){}
- }
- var maxLen=0;
- for each(var o in this.webMailHandlers){
- if(o.enabled){
- if(o.user==defaults[o.id])this.hdlTable[o.id].splice(0,0,o);//default account inserted into index 0
- else this.hdlTable[o.id].push(o);
- if(this.hdlTable[o.id].length>maxLen)maxLen=this.hdlTable[o.id].length;
- o.name=this.getHostName(o.id);
- }
- }
- for(var i in this.hdlTable){
- var o=this.hdlTable[i];
- if(o.length>0){
- var prefStr="accounts."+i+".default";
- var t=null;
- try{
- t=this.prefBranch.getCharPref(prefStr);
- }catch(e){}
- if(t==null)this.prefBranch.setCharPref(prefStr,o[0].user);
- //set account name
- for each(var obj in o){
- if(o.length>1){
- obj.accName=obj.name+" ["+obj.user+"]";
- }else obj.accName=obj.name;
- }
- }
- }
-
- this.workTable=[];
- var cons=Math.max(1,this.prefBranch.getIntPref("connections"));
- for(var n=0;n<maxLen;n+=cons){
- var ar=[];
- this.workTable.push(ar);
- for(var i in this.hdlTable){
- var o=this.hdlTable[i];
- for(var j=n;j<n+cons&&j<o.length;j++){
- ar.push(o[j]);
- }
- }
- }
- if(aCheckNow){
- this.setState(nsIWebMailNotifier.ST_RESET,0);
- this.checkMails();
- }
- }
- WebMailNotifier.prototype.isDefault = function(id,user) {
- return this.hdlTable[id].length==1||(this.hdlTable[id].length>1&&this.hdlTable[id][0].user==user);
- }
-
- WebMailNotifier.prototype.isOpenable = function(id,user) {
- if(this.hdlTable[id].length==1)return true;
- else if(this.hdlTable[id].length==0)return false;
- var ar=this.hdlTable[id];
- for(var i=0;i<ar.length;i++){
- if(ar[i].user==user)break;
- if(ar[i].calcCount()>0)return false;
- }
- return true;
- }
- WebMailNotifier.prototype.addScript = function(id) {
- var rs=this.scripts[id];
- if(id.indexOf("lib-")==0){
- try{
- var loader = Components.classes["@mozilla.org/moz/jssubscript-loader;1"]
- .getService(Components.interfaces.mozIJSSubScriptLoader);
- loader.loadSubScript(this.dir1+id+".js");
- }catch(e){
- dout("load library "+e);
- }
- }else{
- this.scripts[id]=null;
- }
- this.userScripts[id]=true;
- return rs?true:false; //'return rs' returns wrong value
- }
-
- WebMailNotifier.prototype.getHostIDs = function(count) {
- var ar=[];
- for(var i in this.scripts){
- var o={id:i};
- if(this.order[i])o.key=this.order[i].toString();
- ar.push(o);
- }
- ar.sort(this.sortFunc);
- var ar2=[];
- for each(var o in ar)ar2.push(o.id);
- count.value=ar2.length;
- return ar2;
- }
- WebMailNotifier.prototype.getScriptList = function(count) {
- var ar=[];
- for(var i in this.userScripts){
- ar.push(i);
- }
- count.value=ar.length;
- return ar;
- }
- WebMailNotifier.prototype.isInstalled = function(id) {
- for(var i in this.userScripts){
- if(i==id)return true;
- }
- return false;
- }
- WebMailNotifier.prototype.getAccountsInHost = function(id) {
- return this.hdlTable[id].length;
- }
- WebMailNotifier.prototype.getHostName = function(id) {
- try{
- return this.bundle.GetStringFromName(id);
- }catch(e){}
- try{
- var obj=this.loadScript(id,true);
- return obj.name?obj.name:id;
- }catch(e){
- dout("getHostName:"+id+" "+e);
- return id;
- };
- }
- WebMailNotifier.prototype.getScriptVal = function(id,name) {
- try{
- var obj=this.loadScript(id,true);
- return obj[name];
- }catch(e){
- return null;
- };
- }
- WebMailNotifier.prototype.getAccountsNumber = function() {
- return this.webMailHandlers.length;
- }
- WebMailNotifier.prototype.getAccountInfo = function(count,aIndex) {
- var obj=this.webMailHandlers[aIndex];
- var ar=[obj.id,obj.user,obj.password];
- count.value=ar.length;
- return ar;
- }
- WebMailNotifier.prototype.isAccountEnabled = function(aIndex) {
- return this.webMailHandlers[aIndex].enabled;
- }
- WebMailNotifier.prototype.getIntValue = function(name) {
- return this[name];
- }
- WebMailNotifier.prototype.setIntValue = function(name,val) {
- this[name]=val;
- }
-
- WebMailNotifier.prototype.addListener = function(aListener) {
- this.listeners.push(aListener);
- if(!this.inited){
- if(!this.initCalled){
- this.initCalled=true;
- this.init();
- for each(var l in this.listeners){
- l.onStateChange(nsIWebMailNotifier.ST_INIT,0,null);
- for each(var o in this.webMailHandlers){
- if(o.enabled){
- l.onStateChange(o.ind,o.calcCount(),encodeObject(o.mailData));
- }
- }
- }
- this.inited=true;
- }
- }else{
- aListener.onStateChange(nsIWebMailNotifier.ST_INIT,0,null);
- for each(var o in this.webMailHandlers){
- if(o.enabled){
- aListener.onStateChange(o.ind,o.calcCount(),encodeObject(o.mailData));
- }
- }
- }
- }
- WebMailNotifier.prototype.removeListener = function(aListener){
- for(var i=0;i<this.listeners.length;i++){
- if(this.listeners[i]==aListener){
- this.listeners.splice(i,1);
- break;
- }
- }
- }
- WebMailNotifier.prototype.checkMail = function(aIndex) {
- var o=this.webMailHandlers[aIndex];
- o.runCount=0;//used in webupdate notifier
- o.checkMail();
- }
- WebMailNotifier.prototype.stopAll = function() {
- this.timer.cancel();
- this.workTimer.cancel();
- for each(var o in this.webMailHandlers){
- if(o.started)o.stop();
- }
- }
- WebMailNotifier.prototype.checkMails = function(isTimer) {
- if(this.workTable.length>1){
- this.workTimer.cancel();
- this.workTimer.initWithCallback(this.worker,
- this.connectionDelay,
- Components.interfaces.nsITimer.TYPE_ONE_SHOT);
- }
- for each(var o in this.webMailHandlers){
- if(o.enabled){
- o.started=true;
- if(!isTimer)o.runCount=0;
- }
- }
- this.workDone=1;
- for each(var o in this.workTable[0]){
- o.checkMail();
- }
- this.setTimer();
- }
- WebMailNotifier.prototype.timerWork = function(aTimer) {
- for each(var o in this.workTable[this.workDone]){
- o.checkMail();
- }
- ++this.workDone;
- if(this.workDone<this.workTable.length){
- this.workTimer.initWithCallback(this.worker,
- this.connectionDelay,
- Components.interfaces.nsITimer.TYPE_ONE_SHOT);
- }
- }
- WebMailNotifier.prototype.getHandler = function(aID,aUser) {
- for each(var i in this.webMailHandlers){
- if(i.id==aID&&i.user==aUser)return i;
- }
- return null;
- }
- WebMailNotifier.prototype.getMailURL = function(aID,aUser,aFolder) {
- var obj=this.getHandler(aID,aUser);
- if(obj){
- if(this.autoLogin&&!obj.noCookie)obj.cookieManager.setCookieToBrowser();
- if(this.newMailsOnly){
- var url=obj.getMailURL(aFolder);
- var pref="accounts."+aID+"."+aUser+".count";
- try{
- this.prefBranch.getIntPref(pref);//set only if this exists
- this.prefBranch.setIntPref(pref,obj.mailCount>0?obj.mailCount:0);
- }catch(e){}
- obj.mailData.desc=obj.getDesc();
- this.setState(nsIWebMailNotifier.ST_MAILDATA,obj.ind);
- return url;
- }else return obj.getMailURL(aFolder);
- }else return null;
- }
- WebMailNotifier.prototype.getIconURL = function(aID,aUser) {
- var obj=this.getHandler(aID,aUser);
- if(obj)return obj.getIconURL();
- else return null;
- }
- WebMailNotifier.prototype.getMailDomain = function(aID,aUser) {
- var obj=this.getHandler(aID,aUser);
- if(obj&&obj.mailDomain)return obj.mailDomain;
- else return null;
- }
-
- WebMailNotifier.prototype.setState = function(aCmd,aIndex) {
- var aData=null;
- var aCount=0;
- if(aCmd==nsIWebMailNotifier.ST_MAILDATA){
- var o=this.webMailHandlers[aIndex];
- o.started=false;
- aData=encodeObject(o.mailData);
- aCount=o.calcCount();
- var finished=true;
- for each(o in this.webMailHandlers){
- if(o.started){
- finished=false;
- break;
- }
- }
- if(finished){
- var total=0;
- for each(o in this.webMailHandlers){
- if(o.enabled&&o.mailCount>0){
- total+=o.calcCount();
- }
- }
- if(total>this.countTotal)this.notification(total);
- else if(this.countTotal>0&&total==0){
- var win=this.findWindow("alert:alert","wmn_alert");
- if(win)win.close();
- }
- this.countTotal=total;
- //dout("all done");
- }
- }else{//special command
- aCount=aIndex;
- aIndex=aCmd;
- }
- for(var i=0;i<this.listeners.length;i++){
- if(this.listeners[i]){
- this.listeners[i].onStateChange(aIndex,aCount,aData);
- }
- }
- }
- WebMailNotifier.prototype.notification = function(total) {
- if(this.prefBranch.getBoolPref("mailAlert")){
- if(this.listeners.length>0){
- this.showAlertNotification(total);
- }
- }
- if(this.prefBranch.getBoolPref("mailSound")){
- if (!this.mSound)this.mSound = Components.classes["@mozilla.org/sound;1"].createInstance(Components.interfaces.nsISound);
-
- var soundLocation = this.prefBranch.getBoolPref("customSound")?
- this.prefBranch.getCharPref("soundUrl") : "_moz_mailbeep";
- if (soundLocation.indexOf("file://") == -1){
- var os = Components.classes["@mozilla.org/xre/app-info;1"]
- .getService(Components.interfaces.nsIXULRuntime).OS;
- if(os=="Darwin")soundLocation="";
- this.mSound.playSystemSound(soundLocation);
- }else{
- var ioService = Components.classes["@mozilla.org/network/io-service;1"].getService(Components.interfaces.nsIIOService);
- try{
- this.mSound.play(ioService.newURI(soundLocation, null, null));
- }catch(e){}
- }
- }
- }
- WebMailNotifier.prototype.openMails = function(openAll){
- var list=[];
- for each(var o in this.webMailHandlers){
- if(o.enabled){
- if((o.mailCount>0&&o.calcCount()>0&&this.isOpenable(o.id,o.user))
- ||(openAll&&this.isDefault(o.id,o.user))){
- list.push(o);
- }
- }
- }
- for each(var o in list)this.openMail(o.ind);
- }
- WebMailNotifier.prototype.openMail = function(ind,folder){
- var obj=this.webMailHandlers[ind];
- var id=obj.id;
- var user=obj.user;
- var url=this.getMailURL(id,user,folder);//call this for mailCount set
- if(!url)return;
- var mailDomain=this.getMailDomain(id,user);
- var reuse=this.prefBranch.getBoolPref("reuseWindow");
- if(this.prefBranch.getBoolPref("openInTab")){
- this.openTab(url,"wmn_"+id,reuse,mailDomain);
- }else this.openWindow(url,"wmn_"+id,reuse,mailDomain);
- }
- WebMailNotifier.prototype.canReuse = function(cur,mailDomain) {
- if(cur.spec=="about:blank"||!mailDomain)return true;
- var host=null;
- try{
- host=cur.host;
- }catch(e){}
- if(!host)return false;
- try{
- var reg=new RegExp(mailDomain);
- if(!host.match(reg))return false;
- }catch(e){}
- return true;
- }
-
- WebMailNotifier.prototype.findWindow = function(type,name){
- var wm = Components.classes["@mozilla.org/appshell/window-mediator;1"]
- .getService(Components.interfaces.nsIWindowMediator);
- var enm = wm.getEnumerator(type);
- while(enm.hasMoreElements()){
- var win = enm.getNext();
- if(win.name==name)return win;
- }
- return null;
- }
- WebMailNotifier.prototype.openDialog = function(url, name, features) {
- return this.getWindow().openDialog(url,name,features,arguments[3],arguments[4],arguments[5]);
- }
- WebMailNotifier.prototype.openCaptchaDialog = function(host,user,arg) {
- var params = {inn:{host:this.getHostName(host),user:user,secimage:arg}, out:null};
- var features = "chrome,titlebar,toolbar,centerscreen,modal";
- this.openDialog("chrome://wmn/content/captcha.xul","wmn_"+host+":"+user,features,params).focus();
- if (params.out)return params.out.secword;
- else return null;
- }
- WebMailNotifier.prototype.showAlertNotification = function(total){
- this.openDialog("chrome://wmn/content/alert.xul","wmn_alert",
- "chrome,dialog=yes,titlebar=no,popup=yes",
- this.webMailHandlers,this).focus();
- }
- WebMailNotifier.prototype.openWMN = function(){
- this.openDialog("chrome://wmn/content/wmn-window.xul","wmn_win",
- "chrome,toolbar=yes,dialog=no,resizable").focus();
- }
- WebMailNotifier.prototype.openOptions = function() {
- var prefService = Components.classes["@mozilla.org/preferences-service;1"]
- .getService(Components.interfaces.nsIPrefService);
- var branch = prefService.getBranch("browser.preferences.");
- branch.QueryInterface(Components.interfaces.nsIPrefBranch2);
- var instantApply= branch.getBoolPref("instantApply");
- var features = "chrome,titlebar,toolbar,centerscreen" + (instantApply ? ",dialog=no" : ",modal");
- this.openDialog("chrome://wmn/content/options.xul","wmn-pref",features).focus();
- }
- WebMailNotifier.prototype.openInTab = function(url){
- this.openTab(url,null,false,null);
- }
- /***********************************************************
- timer
- ***********************************************************/
- WebMailNotifier.prototype.setTimer = function() {
- this.timer.cancel();
- if(this.timerDelay>0){
- try{
- this.timer.initWithCallback(this,
- this.timerDelay,
- Components.interfaces.nsITimer.TYPE_ONE_SHOT);
- }catch(e){}
- }
- }
-
- WebMailNotifier.prototype.notify = function(aTimer) {
- this.checkMails(true);
- }
- /***********************************************************
- preference observer
- ***********************************************************/
- WebMailNotifier.prototype.observe=function(aSubject, aTopic, aData)
- {
- switch(aTopic){
- case "nsPref:changed":
- switch (aData) {
- case "updateInterval":
- this.timerDelay=this.prefBranch.getIntPref(aData)*60000;
- this.setTimer();
- break;
- case "newMailsOnly":
- this.newMailsOnly=this.prefBranch.getBoolPref("newMailsOnly");
- for each(var o in this.webMailHandlers){
- if(o.enabled){
- /*if(this.newMailsOnly){
- this.prefBranch.setIntPref("accounts."+o.id+"."+o.user+".count",0);
- }else{*/
- if(!this.newMailsOnly){
- try{
- this.prefBranch.clearUserPref("accounts."+o.id+"."+o.user+".count");
- o.mailData.desc=o.getDesc();
- }catch(e){
- dout(e);
- }
- }
- this.setState(nsIWebMailNotifier.ST_MAILDATA,o.ind);
- }
- }
- break;
- case "showStatusbarIcon":
- this.setState(nsIWebMailNotifier.ST_SHOWICON,this.prefBranch.getBoolPref("showStatusbarIcon"));
- break;
- }
- break;
- case "http-on-modify-request":
- for each(var i in this.webMailHandlers){
- if(i.channel==aSubject){
- i.onRequest(aSubject.QueryInterface(Components.interfaces.nsIHttpChannel));
- break;
- }
- }
- break;
- case "http-on-examine-response":
- for each(var i in this.webMailHandlers){
- if(i.channel==aSubject){
- i.onResponse(aSubject.QueryInterface(Components.interfaces.nsIHttpChannel));
- break;
- }
- }
- break;
- case "em-action-requested":
- aSubject.QueryInterface(Components.interfaces.nsIUpdateItem);
- if(aSubject.id == "{37fa1426-b82d-11db-8314-0800200c9a66}") {
- if (aData == "item-uninstalled") {
- this.uninstall = true;
- } else if (aData == "item-cancel-action") {
- this.uninstall = false;
- } else if(aData == "item-installed" || aData == "item-upgraded"){// "item-installed" doesn't work
- this.postInstall();
- }
- }
- break;
- case "quit-application-granted":
- if(this.uninstall){
- var str="chrome://wmn/";
- if(this.loginManager){
- var logins = this.loginManager.getAllLogins({});
- for each (var o in logins) {
- if(o.hostname.indexOf(str)==0)this.loginManager.removeLogin(o);
- }
- }else{
- var e = this.passwordManager.enumerator;
- while (e.hasMoreElements()) {
- var o = e.getNext().QueryInterface(Components.interfaces.nsIPassword);
- if (o.host.indexOf(str)==0)this.passwordManager.removeUser(o.host,o.user);
- }
- }
-
- var prefService = Components.classes["@mozilla.org/preferences-service;1"]
- .getService(Components.interfaces.nsIPrefService);
- var prefBranch = prefService.getBranch("");
- try{ prefBranch.deleteBranch("extensions.wmn."); }catch(e){}
- prefService.savePrefFile(null);
-
- var ios = Components.classes["@mozilla.org/network/io-service;1"]
- .getService(Components.interfaces.nsIIOService);
- var fileHandler = ios.getProtocolHandler("file")
- .QueryInterface(Components.interfaces.nsIFileProtocolHandler);
- var file=fileHandler.getFileFromURLSpec(this.dir1);
- try{file.remove(true);}catch(e){}
- }
- break;
- }
- }
- WebMailNotifier.prototype.postInstall=function(){
- var s1="accounts.wmn_forum.default.enabled";
- var s2="chrome://wmn/accounts/wmn_forum";
- var val=null;
- try{ val=this.prefBranch.getBoolPref(s1); }catch(e){};
- var restart=false;
- if(this.getPassword(s2,"default")==null){
- this.setPassword(s2,"default","1");
- this.prefBranch.setBoolPref(s1,false);
- restart=true;
- }
- //import previous version
- var prefService = Components.classes["@mozilla.org/preferences-service;1"]
- .getService(Components.interfaces.nsIPrefService);
- var pb = prefService.getBranch("");
- var str="chrome://wm-notifier/webmail";
- var str2="chrome://wmn/accounts/";
- var hosts=["daum","naver",null,"nate","gmail","yahoo","hotmail"];
- if(this.loginManager){
- for(var i=0;i<hosts.length;i++){
- var logins = this.loginManager.findLogins({},str+i,formSubmitURL, null);
- for each (var o in logins) {
- var host=hosts[i];
- if(host){
- restart=true;
- this.setPassword(str2+host,o.username,o.password);
- this.removePassword(str+i,o.username);
- try{
- var enabled=pb.getBoolPref("wm-notifier.webmail"+i+"."+o.username+".enabled");
- pb.setBoolPref("extensions.wmn.accounts."+host+"."+o.username+".enabled",enabled);
- }catch(e){}
- }
- }
- }
- }else{
- var e = this.passwordManager.enumerator;
- while (e.hasMoreElements()) {
- try {
- var pass = e.getNext().QueryInterface(Components.interfaces.nsIPassword);
- if (pass.host.indexOf(str)==0) {
- var i=pass.host.substring(str.length);
- var host=hosts[i];
- if(host){
- restart=true;
- this.setPassword(str2+host,pass.user,pass.password);
- this.removePassword(str+i,pass.user);
- try{
- var enabled=pb.getBoolPref("wm-notifier.webmail"+i+"."+pass.user+".enabled");
- pb.setBoolPref("extensions.wmn.accounts."+host+"."+pass.user+".enabled",enabled);
- }catch(e){}
- }
- }
- }catch(ex) {}
- }
- }
- //delete userscript
- this.deleteFile("wmn/aol.js");
- this.deleteFile("wmn/paran.js");
- if(restart)this.initHandlers(true);
- }
-
- /***********************************************************
- password manager
- ***********************************************************/
- WebMailNotifier.prototype.getPassword=function(host,user){
- if(this.loginManager){
- var logins = this.loginManager.findLogins({},host,formSubmitURL, null);
- for each (var o in logins) {
- if (o.username==user) {
- return o.password;
- }
- }
- }else{
- var e = this.passwordManager.enumerator;
- while (e.hasMoreElements()) {
- try {
- var pass = e.getNext().QueryInterface(Components.interfaces.nsIPassword);
- if (pass.host == host&&pass.user == user) {
- return pass.password;
- }
- }catch(ex) {}
- }
- }
- return null;
- }
- WebMailNotifier.prototype.setPassword=function(host,user,passwd){
- if(this.loginManager){
- var nsLoginInfo = new Components.Constructor("@mozilla.org/login-manager/loginInfo;1",
- Components.interfaces.nsILoginInfo,
- "init");
- var loginInfo = new nsLoginInfo(host, formSubmitURL, null, user, passwd, "", "");
-
- var logins = this.loginManager.findLogins({},host,formSubmitURL, null);
- for each (var o in logins) {
- if (o.username==user) {
- this.loginManager.modifyLogin(o,loginInfo);
- return;
- }
- }
- this.loginManager.addLogin(loginInfo);
- }else{
- try{
- this.passwordManager.removeUser(host,user);
- }catch(e){}
- this.passwordManager.addUser(host,user,passwd);
- }
- }
- WebMailNotifier.prototype.removePassword=function(host,user){
- if(this.loginManager){
- var logins = this.loginManager.findLogins({},host,formSubmitURL, null);
- for each (var o in logins) {
- if (o.username==user) {
- this.loginManager.removeLogin(o);
- break;
- }
- }
- }else{
- try{
- this.passwordManager.removeUser(host,user);
- }catch(e){}
- }
- }
- WebMailNotifier.prototype.loadFile=function(name){
- var file = Components.classes["@mozilla.org/file/directory_service;1"]
- .getService(Components.interfaces.nsIProperties)
- .get("ProfD", Components.interfaces.nsIFile);
- name=name.split("/");
- for each(var o in name)file.append(o);
- return this.loadFile0(file);
- }
-
- WebMailNotifier.prototype.loadFile0=function(file){
- var str="";
- try {
- var fis = Components.classes["@mozilla.org/network/file-input-stream;1"]
- .createInstance(Components.interfaces.nsIFileInputStream);
- fis.init(file, 0x01, 0664, 0);
- var is = Components.classes["@mozilla.org/intl/converter-input-stream;1"]
- .createInstance(Components.interfaces.nsIConverterInputStream);
- is.init(fis, "UTF-8", 1024, Components.interfaces.nsIConverterInputStream.DEFAULT_REPLACEMENT_CHARACTER);
-
- var s = {};
- while (is.readString(4096, s) != 0) {
- str+=s.value;
- }
- is.close();
- fis.close();
- }catch(e){
- dout(e);
- }
- return str;
- }
- WebMailNotifier.prototype.saveFile=function(name,str,append){
- var file = Components.classes["@mozilla.org/file/directory_service;1"]
- .getService(Components.interfaces.nsIProperties)
- .get("ProfD", Components.interfaces.nsIFile);
- name=name.split("/");
- for(var i in name){
- file.append(name[i]);
- if(i<name.length-1){
- if(!file.exists()){
- file.create(Components.interfaces.nsIFile.DIRECTORY_TYPE,0755);
- }
- }
- }
- return this.saveFile0(file,str,append);
- }
- WebMailNotifier.prototype.saveFile0=function(file,str,append){
- try{
- var fos = Components.classes["@mozilla.org/network/file-output-stream;1"]
- .createInstance(Components.interfaces.nsIFileOutputStream);
- fos.init(file, 0x02 | 0x08 |(append?0x10:0x20), 0664, 0);
- var os = Components.classes["@mozilla.org/intl/converter-output-stream;1"]
- .createInstance(Components.interfaces.nsIConverterOutputStream);
- os.init(fos, "UTF-8", 0, 0x0000);
- os.writeString(str);
- os.close();
- fos.close();
- }catch(e){
- dout(e);
- }
- return file;
- }
- WebMailNotifier.prototype.deleteFile=function(name){
- var file = Components.classes["@mozilla.org/file/directory_service;1"]
- .getService(Components.interfaces.nsIProperties)
- .get("ProfD", Components.interfaces.nsIFile);
- name=name.split("/");
- for each(var o in name)file.append(o);
- if(file.exists()){
- file.remove(false);
- }
- }
- WebMailNotifier.prototype.removeHost=function(aHostID){
- try{
- this.prefBranch.deleteBranch("accounts."+aHostID);
- var prefService = Components.classes["@mozilla.org/preferences-service;1"]
- .getService(Components.interfaces.nsIPrefService);
- prefService.savePrefFile(null);
- }catch(e){
- dout(e);
- }
- for each(var o in this.webMailHandlers){
- if(o.id==aHostID){
- this.removePassword("chrome://wmn/accounts/"+o.id,o.user);
- }
- }
- delete this.scripts[aHostID];
- delete this.userScripts[aHostID];
- }
- /***********************************************************
- class factory
-
- This object is a member of the global-scope Components.classes.
- It is keyed off of the contract ID.
- ***********************************************************/
- var WebMailNotifierFactory = {
- createInstance: function (aOuter, aIID)
- {
- if (aOuter != null)
- throw Components.results.NS_ERROR_NO_AGGREGATION;
- return (new WebMailNotifier()).QueryInterface(aIID);
- }
- };
-
- /***********************************************************
- module definition (xpcom registration)
- ***********************************************************/
- var WebMailNotifierModule = {
- registerSelf: function(aCompMgr, aFileSpec, aLocation, aType)
- {
- aCompMgr = aCompMgr.
- QueryInterface(Components.interfaces.nsIComponentRegistrar);
- aCompMgr.registerFactoryLocation(CLASS_ID, CLASS_NAME,
- CONTRACT_ID, aFileSpec, aLocation, aType);
- },
-
- unregisterSelf: function(aCompMgr, aLocation, aType)
- {
- aCompMgr = aCompMgr.
- QueryInterface(Components.interfaces.nsIComponentRegistrar);
- aCompMgr.unregisterFactoryLocation(CLASS_ID, aLocation);
- },
-
- getClassObject: function(aCompMgr, aCID, aIID)
- {
- if (!aIID.equals(Components.interfaces.nsIFactory))
- throw Components.results.NS_ERROR_NOT_IMPLEMENTED;
-
- if (aCID.equals(CLASS_ID))
- return WebMailNotifierFactory;
-
- throw Components.results.NS_ERROR_NO_INTERFACE;
- },
- canUnload: function(aCompMgr) { return true; }
- };
-
- /***********************************************************
- module initialization
-
- When the application registers the component, this function
- is called.
- ***********************************************************/
- function NSGetModule(aCompMgr, aFileSpec) { return WebMailNotifierModule; }
-
- /***********************************************************
- CookieManager definition
- ***********************************************************/
- function CookieInfo(){
- }
- CookieInfo.prototype={
- getDomain:function(){
- if(this.domain){
- if(this.domain.charAt(0)!=".")return "."+this.domain;
- else return this.domain;
- }
- return this.uri.host;
- },
- getPath:function(){
- if(this.path)return this.path;
- var path=this.uri.path;
- var i=path.lastIndexOf("/");
- if(i==-1)return path;
- else return path.substring(0,i+1);
- },
- getExpiry:function(){
- if(!this.expires)return 0x7ffffffffffffdff;
- var exp=this.expires;
- exp=exp.replace(/-(.+?)-(\d{2}) /," $1 20$2 ").replace(/-/g," ");
- return Date.parse(exp)/1000;
- },
- toString:function(){
- var s=this.name+"="+this.value;
- for(var p in this){
- if(typeof this[p] == "function")continue;
- if(p=="httponly"||p=="secure"){
- s+="; "+p;
- }else if(p!="name"&&p!="value"&&p!="uri"){
- s+="; "+p+"="+this[p];
- }
- }
- return s;
- }
- }
- function CookieManager(){
- this.cookies=[];
- if("@mozilla.org/xre/app-info;1" in Components.classes){
- var appInfo = Components.classes["@mozilla.org/xre/app-info;1"]
- .getService(Components.interfaces.nsIXULAppInfo);
- var versionChecker = Components.classes["@mozilla.org/xpcom/version-comparator;1"]
- .getService(Components.interfaces.nsIVersionComparator);
- this.isNew=(versionChecker.compare(appInfo.platformVersion, "1.9") >= 0);
- }else{
- this.isNew=false;
- }
- }
- CookieManager.prototype={
- addCookies: function(aURI,aCookie){
- if(typeof(aURI)=="string"){
- var ioService = Components.classes["@mozilla.org/network/io-service;1"]
- .getService(Components.interfaces.nsIIOService);
- aURI=ioService.newURI(aURI,null,null);
- }
- aCookie=aCookie.split("\n");
- for each(var o in aCookie){
- var val=new CookieInfo();
- var ar=o.split(";");
- var t=ar[0].match(/(\S+?)=(.*)/);
-
- val["name"]=t[1];
- val["value"]=t[2];
-
- for(var i=1;i<ar.length;i++){
- if(ar[i]){
- var t=ar[i].replace( /^\s+/g,"");
- var f=t.match(/(\S+?)=(.*)/);
- if(f)val[f[1].toLowerCase()]=f[2];
- else{
- t=t.toLowerCase();
- if(t=="httponly"||t=="secure")val[t]=true;
- else val[t]="";
- }
- }
- }
- val.uri=aURI;
- this._insert(val);
- //if(val.getPath()!=val.path)dout(val.getDomain()+" "+val.getPath()+" "+val.domain+" "+val.path);
- }
- },
- _insert: function(val){
- for(var i in this.cookies){
- var o=this.cookies[i];
- if(o.name==val.name&&o.getDomain()==val.getDomain()&&o.getPath()==val.getPath()){
- this.cookies[i]=val;
- return;
- }
- }
- this.cookies.push(val);
- },
- findCookie:function(urispec,name){
- for each(var o in this.cookies){
- if(o.uri.spec==urispec&&o.name==name){
- return o.toString();
- }
- }
- return null;
- },
- getCookie: function(aURI){
- var str="";
- for each(var o in this.cookies){
- var domain=o.getDomain();
- if(domain.charAt(0)==".")domain=domain.substring(1);
- if(aURI.host.lastIndexOf(domain)==-1)continue;
- if(aURI.path.indexOf(o.getPath())!=0)continue;
- if(o.secure&&aURI.scheme!="https")continue;
- var exp=o.expires;
- if(exp){
- exp=exp.replace(/-(.+?)-(\d{2}) /," $1 20$2 ").replace(/-/g," ");
- if(Date.parse(exp)<new Date()){
- continue;
- }
- }
- var s=o.name+"="+o.value;
- if(str)str+="; "+s;
- else str=s;
- }
- return str;
- },
- setCookieToBrowser: function(){
- var cm = Components.classes["@mozilla.org/cookiemanager;1"]
- .getService(Components.interfaces.nsICookieManager2);
- for each(var o in this.cookies){
- //if(!o.expires)dout(o.name+" "+o.getExpiry());
- if (this.isNew){
- cm.add(o.getDomain(),o.getPath(),o.name,o.value,o.secure,o.httponly,!o.expires,o.getExpiry());
- }else{
- cm.add(o.getDomain(),o.getPath(),o.name,o.value,o.secure,!o.expires,o.getExpiry());
- }
- }
- },
- clear: function(){
- this.cookies=[];
- }
- }
- /***********************************************************
- WebMailHandler definition
- ***********************************************************/
- function WebMailHandler(webMailNotifier){
- this.main=webMailNotifier;
- this.cookieManager=new CookieManager();
- this.loggedIn=false;
- this.reset();
- }
- WebMailHandler.prototype={
- charSet: null,
- data: "",
- stage: 0,
- initStage: ST_LOGIN,
- mailCount:-1,
- mailData:{},
- user: "",
- password: null,
- loginData:[],
- started:false,
- // nsIChannelEventSink
- onChannelRedirect: function (aOldChannel, aNewChannel, aFlags) {
- if(this.channel==aOldChannel)this.channel = aNewChannel;
- },
- // nsIInterfaceRequestor
- getInterface: function (aIID) {
- try {
- return this.QueryInterface(aIID);
- } catch (e) {
- //dout("getInterface "+aIID);
- throw Components.results.NS_NOINTERFACE;
- }
- },
- // nsIProgressEventSink
- onProgress : function (aRequest, aContext, aProgress, aProgressMax) { },
- onStatus : function (aRequest, aContext, aStatus, aStatusArg) { },
- // nsIHttpEventSink
- onRedirect : function (aOldChannel, aNewChannel) { },
- // nsIAuthPromptProvider
- getAuthPrompt : function(aReason){ return null; },
- QueryInterface : function(aIID) {
- if (aIID.equals(Components.interfaces.nsISupports) ||
- aIID.equals(Components.interfaces.nsIInterfaceRequestor) ||
- aIID.equals(Components.interfaces.nsIChannelEventSink) ||
- aIID.equals(Components.interfaces.nsIProgressEventSink) ||
- aIID.equals(Components.interfaces.nsIHttpEventSink) ||
- aIID.equals(Components.interfaces.nsIStreamListener)||
- aIID.equals(Components.interfaces.nsIAuthPromptProvider)||
- aIID.equals(Components.interfaces.nsIPrompt)|| //not used but required in ff 2.0
- aIID.equals(Components.interfaces.nsIDocShell)|| //required in ff 3.0
- aIID.equals(Components.interfaces.nsIAuthPrompt))
- return this;
- //dout("QueryInterface "+aIID);
- throw Components.results.NS_NOINTERFACE;
- },
- onStartRequest : function (aRequest, aContext) {
- this.data = "";
- },
- onDataAvailable : function (aRequest, aContext, aStream, aSourceOffset, aLength){
- var is = Components.classes["@mozilla.org/scriptableinputstream;1"]
- .createInstance(Components.interfaces.nsIScriptableInputStream);
- is.init(aStream);
- this.data += is.read(aLength);
- },
- onStopRequest : function (aRequest, aContext, aStatus) {
- if(aStatus==Components.results.NS_BINDING_ABORTED)return;
- var charSet=null;
- var httpChannel=aRequest.QueryInterface(Components.interfaces.nsIHttpChannel);
- if(this.charSet)charSet=this.charSet;
- else{
- var fnd=this.data.match(/<meta.+?\".+?charset=(\S+?)\s*?;?\s*?\"\s*?\x2f?>/);
- if(fnd)charSet=fnd[1];
- else{
- try{
- if(httpChannel.contentCharset)charSet=httpChannel.contentCharset;
- }catch(e){}
- }
- }
- if(charSet){
- var uniConv = Components.classes["@mozilla.org/intl/scriptableunicodeconverter"]
- .createInstance(Components.interfaces.nsIScriptableUnicodeConverter);
- uniConv.charset = charSet;
- try{
- this.data=uniConv.ConvertToUnicode(this.data);
- }catch(e){}
- }
- try{
- if(!this.process(aContext.QueryInterface(Components.interfaces.nsIHttpChannel), this.data))++this.stage;
- }catch(e){
- this.onError();
- dout(this.id+" "+e);
- }
- },
- stop : function(){
- if(this.channel){
- this.channel.cancel(Components.results.NS_BINDING_ABORTED);
- }
- },
- process : function (aHttpChannel, aData){
- switch(this.stage){
- case ST_LOGIN:
- this.getHtml(this.loginData[LOGIN_URL],this.loginData[LOGIN_POST]);
- return false;
- case ST_LOGIN_RES:
- this.stage=ST_DATA;
- case ST_DATA:
- if(!this.dataURLCopy){
- if(this.allDataURL&&!this.isInboxOnly())this.dataURLCopy=this.allDataURL;
- else this.dataURLCopy=this.dataURL;
- }
- this.getHtml(this.dataURLCopy);
- return false;
- case ST_DATA_RES:
- this.mailCount=this.getCount(aData);
- this.mailData=this.getData(aData);
- this.mailData.desc=this.getDesc();
- try{
- if(this.mailCount>=0&&aHttpChannel!=null&&aHttpChannel.responseStatus==302){
- /*var l=aHttpChannel.getResponseHeader("Location");
- if(l.indexOf("http")==0){
- this.dataURLCopy=l;
- }else{
- if(l.indexOf("/")==0){
- this.dataURLCopy=aHttpChannel.URI.scheme+"://"+aHttpChannel.URI.hostPort+l;
- }
- }*/
- this.dataURLCopy=this.channel.URI.spec;
- }
- }catch(e){
- dout(e);
- }
- if(this.mailCount<0)this.reset();
- else this.stage=ST_DATA;
- this.main.setState(nsIWebMailNotifier.ST_MAILDATA,this.ind);
- if(this.main.autoLogin){
- if(this.mailCount>=0&&!this.loggedIn&&this.main.isDefault(this.id,this.user)){
- this.loggedIn=true;
- this.cookieManager.setCookieToBrowser();
- }
- }
- return true;
- }
- dout("[onError] "+this.id+" "+this.user+" "+this.stage);
- this.onError();
- return true;
- },
- onError : function(){
- this.reset();
- this.main.setState(nsIWebMailNotifier.ST_MAILDATA,this.ind);
- },
- reset : function(){
- this.mailCount=-1;
- this.mailData={};
- this.mailData.desc=this.getDesc();
- this.stage=this.initStage;
- this.dataURLCopy=null;
- this.cookieManager.clear();
- },
- checkMail : function(){
- if(!this.enabled)return;
- this.stop();
- //this.stage=this.mailCount<0?this.initStage:ST_DATA;
- if(this.mailCount<0)this.reset();
- else this.stage=ST_DATA;
- this.started=true;
- try{
- if(!this.process(null,""))++this.stage;
- }catch(e){
- this.onError();
- dout(this.id+" "+e);
- }
- },
- onRequest : function(aHttpChannel){
- var ck=this.cookieManager.getCookie(aHttpChannel.URI);
- aHttpChannel.setRequestHeader("Cookie",ck, false);
- },
- onResponse : function(aHttpChannel){
- try {
- var cookie = aHttpChannel.getResponseHeader("Set-Cookie");
- this.cookieManager.addCookies(aHttpChannel.URI,cookie);
- aHttpChannel.setResponseHeader("Set-Cookie", "", false);
- }catch(e){}
- },
- getMailURL : function(aFolder){
- return this.mailURL;
- },
- getIconURL : function(){
- try{
- var url=this.mailURL.match(/(((\S+):\/\/([^/]+))(\S*\/)?)([^/]*)/);
- if(url)return url[2]+"/favicon.ico";
- }catch(e){}
- return null;
- },
- getData : function(aData){
- return {};
- },
- getDesc : function(){
- var n=this.calcCount();
- return n>=0?n:"";
- },
- calcCount : function() {
- var aCount=this.mailCount;
- if(this.main.newMailsOnly){
- if(aCount>=0){
- var prefStr="accounts."+this.id+"."+this.user+".count";
- var count=null;
- try{
- count=this.main.prefBranch.getIntPref(prefStr);
- }catch(e){}
- if(count==null){
- this.main.prefBranch.setIntPref(prefStr,0);
- count=0;
- }
- if(aCount>=count)aCount-=count;
- else{
- this.main.prefBranch.setIntPref(prefStr,aCount>0?aCount:0);
- aCount=0;
- }
- }
- }
- return aCount;
- }
- };
- WebMailHandler.prototype.baseProcess=WebMailHandler.prototype.process
- WebMailHandler.prototype.baseGetData=WebMailHandler.prototype.getData
- /***********************************************************
- load html
- ***********************************************************/
- WebMailHandler.prototype.getHtml = function(aURL,aPostData,aHeaders) {
- var ioService = Components.classes["@mozilla.org/network/io-service;1"].getService(Components.interfaces.nsIIOService);
- var uri = ioService.newURI(aURL, null, null);
- var channel = ioService.newChannelFromURI(uri);
- var httpChannel = channel.QueryInterface(Components.interfaces.nsIHttpChannel);
- if (aPostData) {
- var uploadStream = Components.classes["@mozilla.org/io/string-input-stream;1"]
- .createInstance(Components.interfaces.nsIStringInputStream);
- uploadStream.setData(aPostData, aPostData.length);
- var uploadChannel = channel.QueryInterface(Components.interfaces.nsIUploadChannel);
- uploadChannel.setUploadStream(uploadStream, "application/x-www-form-urlencoded", -1);
- httpChannel.requestMethod = "POST";
- }
- if(aHeaders){
- for(var t in aHeaders){
- httpChannel.setRequestHeader(t,aHeaders[t],false);
- }
- }
- this.channel=channel;
- channel.notificationCallbacks = this;
- channel.asyncOpen(this,httpChannel);
- }
- WebMailHandler.prototype.isInboxOnly = function(){
- if(!this.supportInboxOnly)return true;
- var s="accounts."+this.id+"."+this.user+".inboxOnly";
- try{
- return this.main.prefBranch.getBoolPref(s);
- }catch(e){
- this.main.prefBranch.setBoolPref(s,true);
- }
- return true;
- }
- WebMailHandler.prototype.isShowFolders = function(){
- if(!this.supportShowFolders)return true;
- var s="accounts."+this.id+"."+this.user+".showFolders";
- try{
- return this.main.prefBranch.getBoolPref(s);
- }catch(e){
- this.main.prefBranch.setBoolPref(s,true);
- }
- return true;
- }
- WebMailHandler.prototype.getCookie = function(domain,name){
- var obj = Components.classes["@mozilla.org/cookiemanager;1"].
- getService(Components.interfaces.nsICookieManager2);
- var enm = obj.enumerator;
- while(enm.hasMoreElements()){
- var ck = enm.getNext();
- ck.QueryInterface(Components.interfaces.nsICookie2);
- if(ck.host.lastIndexOf(domain)!=-1&&ck.name==name&&!ck.isSession)return ck;
- }
- return null;
- };
- WebMailHandler.prototype.getCookieString = function(domain,name){
- var ck=this.getCookie(domain,name);
- if(!ck)return null;
- var s=ck.name+"="+ck.value;
- if(ck.expires!=0)s+="; expires="+new Date(ck.expires*1000).toUTCString();
- if(ck.path)s+="; path="+ck.path;
- if(ck.host)s+="; domain="+ck.host;
- if(ck.secure)s+="; secure";
- if(ck.httponly)s+="; httponly";
- return s;
- };
-
- function encodeObject(data){
- var s="";
- for(var i in data){
- if(s)s+=",";
- s+=escape(i)+":"+escape(data[i]);
- }
- return s;
- }
- function encodeArray(data){
- var s="";
- for(var i=0;i<data.length;i++){
- if(s)s+=",";
- s+=escape(data[i]);
- }
- return s;
- }
- ////////////////for debug//////////////////////////////////
- function dlog(str){
- dout(str);
- var name="wmn-log.txt";
- if(enableDebug==2)this.notFirst=true;
- str=new Date()+"\t"+str.replace(/\r/g,"\\r").replace(/\n/g,"\\n")+"\r\n";
- WebMailNotifier.prototype.saveFile("wmn-log.txt",str,this.notFirst);
- }
- ///////////////////////////////////////////////////////////
-